package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Psycho
 * @create 2022-04-25-16:36
 */
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Autowired
    private WmMaterialService wmMaterialService;

    @PostMapping("/upload_picture")
    public ResponseResult listMaterial(MultipartFile multipartFile) {
        return wmMaterialService.uploadPicture(multipartFile);
    }

    @PostMapping("/list")
    public ResponseResult findList(@RequestBody WmMaterialDto dto){
        return wmMaterialService.findList(dto);
    }

    @GetMapping("/collect/{id}")
    public ResponseResult isCollection(@PathVariable("id")Long id){
        WmMaterial material = wmMaterialService.getById(id);
        material.setIsCollection((short) 1);
        wmMaterialService.updateById(material);
        return ResponseResult.okResult(material);
    }

    @GetMapping("/cancel_collect/{id}")
    public ResponseResult notCollection(@PathVariable("id")Long id){
        WmMaterial material = wmMaterialService.getById(id);
        material.setIsCollection((short) 0);
        wmMaterialService.updateById(material);
        return ResponseResult.okResult(material);
    }
}